package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.dao.SetmealCheckGroupDao;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.Setmeal;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;

    /**
     * 新增套餐
     *
     * @param setmeal
     * @return
     */
    @Override
    public void add(Setmeal setmeal, List<Integer> checkgroupIds) {
        //在setmeal表中新增套餐
        setMealDao.add(setmeal);
        if (!checkgroupIds.isEmpty()) {
            //将套餐和检查组封装到map中传入后端
            Map<String, Object> map = new HashMap<>();
            map.put("setmealId", setmeal.getId());
            map.put("checkgroupIds", checkgroupIds);
            setmealCheckGroupDao.batchInsertSetMeal(map);
        }
    }

    /**
     * 分页查询
     *
     * @param queryPageBeanDTO
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBeanDTO queryPageBeanDTO) {
        PageHelper.startPage(queryPageBeanDTO.getCurrentPage(), queryPageBeanDTO.getPageSize());
        Page<Setmeal> page = setMealDao.pageQuery(queryPageBeanDTO.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }
}
